<template>
  <el-row :gutter="20" style="padding: 20px">
    <el-col :span="spanData">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="手机号">
          <el-input v-model="username" readonly></el-input>
        </el-form-item>
        <el-form-item prop="smsCaptchaResult" label="短信验证码">
          <el-input style="width: 180px; margin-right: 10px" prefix-icon="el-icon-chat-dot-square"
            v-model="ruleForm.smsCaptchaResult" placeholder="短信验证码"></el-input>
          <el-button type="primary" v-if="!countDown"
            @click="sendVerifyCode">发送</el-button>
          <el-button type="primary" :disabled="true" v-else>{{ countDown }}s</el-button>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input placeholder="请输入新密码" v-model="ruleForm.password" auto-complete="new-password"
            type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="conpassword">
          <el-input placeholder="请输入确认密码" v-model="ruleForm.conpassword" type="password"></el-input>
        </el-form-item>
        <el-form-item v-if="!token">
              <vue-drag-verify ref="slideRef" text="向右滑动" successText="成功" :height="28" :width="260" :circle="false" @passcallback="onSuccess"></vue-drag-verify>
            </el-form-item>
        <el-form-item>
          <el-button :disabled="!isSuccess && !token" type="primary" @click="submitForm('ruleForm')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
export default {
  props: {
    userName: {
      type: String,
      default: ''
    },
    spanData: {
      type: Number,
      default: 12
    }
  },
  watch: {
    userName: {
      handler(val) {
        if (val) {
          this.username = val
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      token: localStorage.getItem("token"),
      isSuccess: false,
      uuid: "",
      smsUuid: "",
      codeUrl: "",
      username: localStorage.getItem("username"),
      countDown: 0, // 倒计时
      ruleForm: {
        password: "",
        conpassword: "",
        smsCaptchaResult: "",
        uuid: "",
      },
      rules: {
        password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],
        conpassword: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
        ],
        smsCaptchaResult: [
          {
            required: true,
            message: "请输入手机验证码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    onSuccess() {
      this.isSuccess = true
    },
    // 4.获取手机验证码
    async sendVerifyCode() {
      const params = {
        uuid: this.uuid,
      };
      if (window.location.href.includes('homePage')) {
        params.phone = this.userName
      }
      // 4.2 获取短信验证码
      const url = window.location.href.includes('homePage') ? '/customer/forgetOfSms' : '/customer/updatePwdOfSms'
      this.$post(url, params)
        .then((res) => {
          if (res && res.code * 1 === 200) {
            this.smsUuid = res.data;
            this.$message.success("短信发送成功");
            this.countDown = 60;
            this.timeIntervalID = setInterval(() => {
              this.countDown--;
              // 4.1 如果减到0 则清除定时器
              if (this.countDown == 0) {
                clearInterval(this.timeIntervalID);
              }
            }, 1000);
          } else {
            //
          }
        })
        .catch(() => {
          //
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = {
            password: this.ruleForm.password,
            code: this.ruleForm.smsCaptchaResult,
            conpassword: this.ruleForm.conpassword,
            uuid: this.smsUuid,
          };
          if (window.location.href.includes('homePage')) {
            params.username = this.userName
          }
          const url = window.location.href.includes('homePage') ? '/customer/updateForgetPwd' : '/customer/updatePwd'
          this.$post(url, params).then((res) => {
            if (res && res.code * 1 === 200) {
              this.$message.success("操作成功");
              this.ruleForm = {
                password: "",
                conpassword: "",
                smsCaptchaResult: "",
                uuid: "",
              };

              this.$emit('hidePasswordChange')
            }
          }).finally(() => {
            clearInterval(this.timeIntervalID);
              this.countDown = 0
          })
        } else {
          return false;
        }
      });
    },
  },
  mounted() {
  },
};
</script>
<style scoped lang="less">
.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 38px;
}
</style>